package com.itheima.stargraph.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.stargraph.mapper.SgUserResultMapper;
import com.itheima.stargraph.pojo.SgUserResult;
import com.itheima.stargraph.service.ISgUserResultService;
import com.itheima.stargraph.utils.UserThreadLocalUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 生产的结果信息表 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-06-12
 */
@Service
public class SgUserResultServiceImpl extends ServiceImpl<SgUserResultMapper, SgUserResult> implements ISgUserResultService {

    /**
     * 将生图结果数据批量保存到数据库中
     *
     * @param stringList
     */
    @Override
    public void insertExecured(Long userId,List<String> stringList) {
        //判断参数stringList是否为空
        if (CollUtil.isEmpty(stringList)){
            return;
        }
        //封装数据
        List<SgUserResult> sgUserResultList = stringList.stream().map(x -> {
            SgUserResult sgUserResult = new SgUserResult();
            sgUserResult.setUrl(x);
            sgUserResult.setCollect(0);
            sgUserResult.setUserId(userId);
            return sgUserResult;
        }).collect(Collectors.toList());
        //批量添加
        boolean result = this.saveBatch(sgUserResultList);
        if (!result){
            log.error("批量添加失败,ookk");
        }
    }

    /**
     * 获取生图结果列表数据
     *
     * @return
     */
    @Override
    public List<SgUserResult> resultList() {
        List<SgUserResult> sgUserResultList = this.lambdaQuery().eq(SgUserResult::getUserId, UserThreadLocalUtils.getUser().getId())
                .orderByDesc(SgUserResult::getCreatedTime)
                .list();
        return sgUserResultList;
    }
}
